using System;
using System.Reflection;

namespace Byond.Vst.WpfSendKeys
{
	internal static class ReflectionExtensions
	{
		public static object GetFieldValue(this object instance, string fieldName)
		{
			var type = instance.GetType();
			FieldInfo fieldInfo = null;
			while (type != null)
			{
				fieldInfo = type.GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
				if (fieldInfo != null)
					break;
				type = type.BaseType;
			}

			if (fieldInfo == null)
				throw new FieldAccessException("Field " + fieldName + " was not found on type " + type);
			var result = fieldInfo.GetValue(instance);
			return result; // you can place a breakpoint here (for debugging purposes)
		}
	}
}